﻿using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CatUtils.Utils.Currency
{
    /// <summary>
    /// 打印日志的方法
    /// </summary>
    public partial class PrintLog
    {   /// <summary>
        /// 定位打印，需要指定显示的位置。
        /// </summary>
        /// <param name="Str"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void WriteAt(string Str, int x = 0, int y = 0)
        {
            try
            {
                Console.SetCursorPosition(x, y);
                Console.Write(Str);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.Clear();
                Console.WriteLine(GetExceptionMessage(e));
            }
        }

        /// <summary>
        /// 打印百分比Log,如果自定义LogMessage 请设置参数不高于三个，默认打印日志模板:"总数{0}，已处理{1},占比{2}%"
        /// </summary>
        /// <param name="AllCount">百分比总数</param>
        /// <param name="ComplateCount">当前数量</param>
        /// <param name="LogMessage">默认打印日志模板:"总数{0}，已处理{1},占比{2}%"</param>
        public void PercentLog(int AllCount, int ComplateCount, string LogMessage = "总数{0}，已处理{1},占比{2}%")
        {
            PercentLog(AllCount, ComplateCount, true, LogMessage);
            VNoWrite($"{DateTime.Now}:{string.Format(LogMessage, AllCount, ComplateCount, ((float)ComplateCount / AllCount) * 100)}");
        }

        /// <summary>
        /// 打印百分比Log,如果自定义LogMessage 请设置参数不高于三个，默认打印日志模板:"总数{0}，已处理{1},占比{2}%"
        /// </summary>
        /// <param name="AllCount">百分比总数</param>
        /// <param name="ComplateCount">当前数量</param>
        /// <param name="SaveLog">是否保存日志</param>
        /// <param name="LogMessage">默认打印日志模板:"总数{0}，已处理{1},占比{2}%"</param>
        public void PercentLog(int AllCount, int ComplateCount, bool SaveLog, string LogMessage = "总数{0}，已处理{1},占比{2}%")
        {
            if (SaveLog)
                WriteLog($"{DateTime.Now}:{string.Format(LogMessage, AllCount, ComplateCount, ((float)ComplateCount / AllCount) * 100)}");
            else
                VNoWrite($"{DateTime.Now}:{string.Format(LogMessage, AllCount, ComplateCount, ((float)ComplateCount / AllCount) * 100)}");
        }

        /// <summary>
        /// 写入日志，在temp文件夹下，以时间戳命名。用于处理文件访问被占用时写入日志。
        /// </summary>
        /// <param name="Content">日志内容</param>
        /// <param name="Label">随机日志的名字</param>
        public static void RandomLog(string Content, string Label = "随机日志")
        {
            FileHelper.AppendText(StaticValue.TempPath + Label + DateTime.Now.ToFileTime() + ".txt", Content);
        }

        /// <summary>
        /// 保存日志，并且刷新日志list列表
        /// </summary>
        /// <param name="LogPath">日志文件列表</param>
        /// <param name="Text">日志字符串</param>
        /// <param name="LogList">添加进缓存日志数组</param>
        [Obsolete("建议使用SQLLITE进行日志处理。", true)]
        public static void ReflushLog(string LogPath, string Text, List<string> LogList)
        {
            FileHelper.AppendText(LogPath, Text);
            LogList.Add(Text);
        }

        /// <summary>
        /// 缩短日志
        /// </summary>
        /// <param name="FilePath">要缩短行数的文件路径</param>
        /// <param name="Count">日志保留多少航</param>
        public static void LogNarrow(string FilePath, int Count = 3000)
        {
            List<string> TempList = new List<string>();
            foreach (string Line in ListHelper.ListNarrow(FilePath, Count))
            {
                if (Line != "")
                    TempList.Add(Line);
            }
            FileHelper.WriteList(FilePath, TempList);
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="Log">日志内容</param>
        private static void WriteLog(string Log)
        {
            try
            {
                File.AppendAllText(ErrorLogPath, $"\r\n{DateTime.Now}:{Log}", Encoding.UTF8);
            }
            catch (Exception ex)
            {
                FloderHelper.FloderExits(StaticValue.LogFloder, true);
                try
                {
                    string TempLog = $"{Log}\r\n {ex.ToString()}";
                    File.AppendAllText(FileNameHelper.CreateFileNameAddTip(ErrorLogPath, DateTime.Now.ToFileTimeUtc().ToString()), $"\r\n{DateTime.Now }:{TempLog}", Encoding.UTF8);
                }
                catch (Exception ExPlus)
                {
                    Console.WriteLine(ex);
                    Console.WriteLine(ExPlus);
                }
            }
        }
    }
}